package com.xjw.coreutil.http;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TypeReference;

/**
 * @author 徐军文
 * @since 2021/11/6 0:35
 */
public class JsonUtil {


    /**
     * 将一般类变成json数据
     * @param obj 任意类
     * @return string
     */
    public static String objectToString(Object obj) {
        return JSON.toJSONString(obj, JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.WriteNonStringKeyAsString);
    }

    public static <T> T parseBean(String text, Class<T> t) {
        return JSON.parseObject(text,t);
    }

    public static JsonUtil getJson(){
        return  new JsonUtil();
    }

    public static <T> T parseObject(String src, TypeReference type) {
        return (T) JSON.parseObject(src, type);
    }

    public static <T> T parseObject(String src, Class type) {
        return (T) JSON.parseObject(src, type);
    }

}
